package com.cskaoyan.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.vo.CategoryL1VO;
import com.cskaoyan.vo.CategoryList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> queryCategory() {
        //一级查询

        CategoryExample oneCategoryExample = new CategoryExample();
        CategoryExample.Criteria l1 = oneCategoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> oneCategories = categoryMapper.selectByExample(oneCategoryExample);
        //二级查询
        CategoryExample twoCategoryExample = new CategoryExample();
        CategoryExample.Criteria l2 = twoCategoryExample.createCriteria().andLevelEqualTo("L2").andDeletedEqualTo(false);
        List<Category> twoCategories = categoryMapper.selectByExample(twoCategoryExample);

        for (int i = 0; i < oneCategories.size(); i++) {
            for (int j = 0; j < twoCategories.size(); j++) {
                if (oneCategories.get(i).getId().equals(twoCategories.get(j).getPid())) {
                    oneCategories.get(i).getChildren().add(twoCategories.get(j));
                }
            }
        }
        return oneCategories;
    }

    @Override
    public List<Category> queryCategoryL1() {

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria l1 = categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        return categories;
    }

    @Override
    public void deleteCategory(Category category) {

        if (!Objects.isNull(category.getChildren())&&category.getChildren().size()!=0){
            for (int i=0;i<category.getChildren().size();i++){
                Category categoryChild=category.getChildren().get(i);
                categoryChild.setUpdateTime(new Date());
                categoryChild.setDeleted(true);
                categoryMapper.updateByPrimaryKeySelective(categoryChild);
            }
        }
        category.setUpdateTime(new Date());
        category.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public Category create(Category category) {
        Date time = new Date(System.currentTimeMillis());
        category.setAddTime(time);
        category.setUpdateTime(time);
        categoryMapper.insertSelective(category);
        return categoryMapper.selectByPrimaryKey(category.getId());
    }

    @Override
    public void update(Category category) {
        Date time = new Date(System.currentTimeMillis());
        category.setUpdateTime(time);
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public List<CategoryList> querylist() {
//       创建一个存储最后结果的对象
        List<CategoryList> categoryLists = new ArrayList<CategoryList>();
//        首先查询所有的L1的list放入
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        criteria.andLevelEqualTo("L1");
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
//        开始放入
        for (Category category : categories) {
            CategoryList categoryList = new CategoryList();
            categoryList.setId(category.getId());
            categoryList.setName(category.getName());
            categoryList.setKeywords(category.getKeywords());
            categoryList.setDesc(category.getDesc());
            categoryList.setIconUrl(category.getIconUrl());
            categoryList.setPicUrl(category.getPicUrl());
            categoryList.setLevel(category.getLevel());

//          放完基础部分,接下来就是取出对应的pid的children
            CategoryExample categoryExample1 = new CategoryExample();
            CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();

            criteria1.andPidEqualTo(category.getId());
            criteria1.andDeletedEqualTo(false);
            List<Category> categories11 = categoryMapper.selectByExample(categoryExample1);

            ArrayList<CategoryList.ChildrenBean> childrenBeans = new ArrayList<>();
            for (Category category1 : categories11) {
                CategoryList.ChildrenBean childrenBean = new CategoryList.ChildrenBean();
                childrenBean.setId(category1.getId());
                childrenBean.setName(category1.getName());
                childrenBean.setKeywords(category1.getKeywords());
                childrenBean.setDesc(category1.getDesc());
                childrenBean.setIconUrl(category1.getIconUrl());
                childrenBean.setPicUrl(category1.getPicUrl());
                childrenBean.setLevel(category1.getLevel());
                childrenBeans.add(childrenBean);
            }
            categoryList.setChildren(childrenBeans);
            categoryLists.add(categoryList);
        }
        return categoryLists;

    }

    @Override
    public List<CategoryL1VO> queryL1() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();

        criteria.andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        System.out.println(categories);
        List<CategoryL1VO> categoryL1VOS = new ArrayList<>();

        for (Category category : categories) {
            CategoryL1VO categoryL1VO = new CategoryL1VO();
            categoryL1VO.setLabel(category.getName());
            categoryL1VO.setValue(category.getId());
            categoryL1VOS.add(categoryL1VO);
        }
        return  categoryL1VOS;
    }

}
